package com.gyh.stlserver.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.gyh.stlserver.constants.Constants;
import com.gyh.stlserver.util.JWTUtil;
import com.gyh.stlserver.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {
    @GetMapping
    public Result getCaptcha(){
        // 自定义纯数字的验证码（随机4位数字，可重复）
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(randomGenerator);
        // 重新生成code
        lineCaptcha.createCode();
        String code = JWTUtil.createJWT(lineCaptcha.getCode(),  60*60 * 1000L);
        String image = lineCaptcha.getImageBase64Data();
        return Result.success(Constants.STATUS_200,code,image);
    }

}
